package org.goobs.applications;

import java.util.HashSet;

import org.goobs.csp.CSP;
import org.goobs.csp.CSPVariable;
import org.goobs.csp.UnaryConstraint;
import org.goobs.csp.UniquenessConstraint;
import org.goobs.internet.Email;
import org.goobs.internet.MailServer;

public class SecretSanta {
	
	private static class UniqueCons extends UniquenessConstraint{
		@SuppressWarnings("unchecked")
		public UniqueCons(CSPVariable var) {
			super(var);
		}
		public boolean thisVarQualifies(){
			return true;
		}
		@SuppressWarnings("unchecked")
		@Override
		/*
		 * No one else has the same receiver as this variable
		 */
		public boolean otherVarUnique(CSPVariable otherVar) {
			String receiver = this.var.getValue().toString();
			boolean rtn = !receiver.equalsIgnoreCase( otherVar.getValue().toString() );
//			if(!rtn){
//				System.out.println("rejected arc: " + this);
//			}
			return rtn;
		}
	}
	
	@SuppressWarnings("unchecked")
	private static class NonReflexive extends UnaryConstraint{
		public NonReflexive(CSPVariable var) {
			super(var);
		}
		@Override
		public boolean isSatisfied() {
			boolean rtn = !var.getValue().equals(var.getData().toString());
//			if(!rtn){
//				System.out.println("rejected");
//			}
			return rtn;
		}
		
	}
	
	private static final String[] names = {
			"Gabor Angeli",
			"Nick Hatamiya",
			"Bob Bellerose",
			"Albert Kim",
			"Diana Sandoval",
			"Rachel Pollak",
			"Kenny Congrove",
			"Eric Lee"
	};
	
	private static final String[] emails = {
		"gangeli@berkeley.edu",
		"nickhata@berkeley.edu",
		"rjbellerose@berkeley.edu",
		"a_js_kim@berkeley.edu",
		"dsandoval@berkeley.edu",
		"rpollak@berkeley.edu",
		"kwcon3@berkeley.edu",
		"elee88@berkeley.edu"
	};
	
	private static final MailServer server = new MailServer("gaasanalytical.com", "gaasanalytical.com", MailServer.POP);
	
	private static void handleSanta(String gName, String gEmail, String rName, String rEmail){
		Email toSend = new Email( "webmaster@gaasanalytical.com" );
		toSend.setFrom("gangeli@berkeley.edu");
		toSend.setSubject("2A10+Friends Secret Santa");
		toSend.setText(gName +",\n" +
				"   Hey, ");
	}
	
	
	public static void main(String[] args){		
		System.out.println("Setting up CSP...");
		CSP santa = new CSP();
		
		System.out.println("   creating domain...");
		final HashSet <String> nameSet = new HashSet <String> ();
		for( String name : names ){
			nameSet.add(name);
		}
		
		System.out.println("   adding variables and basic constraints...");
		for(String name : nameSet){
			System.out.println("      " + name);
			CSPVariable <String> var = new CSPVariable <String> (nameSet, name);
			santa.addVariable(var);
			santa.addConstraint(new UniqueCons(var));
			santa.addConstraint(new NonReflexive(var));
		}
		
		System.out.println();
		santa.printState();
		
		System.out.println("Running CSP...");
		if(!santa.register()){
			System.out.println("   COULD NOT REGISTER CSP");
			System.exit(1);
		}

		int i=0;
		while(santa.findSolution()){
//			santa.printState();
			i++;
			if(i % 1000 == 0){
				santa.printState();
			}
		}
		System.out.println("found " + i + " solutions");
//		boolean good = santa.findSolution();
//		if(!good){
//			System.out.println("   NO SOLUTION!");
//			System.exit(1);
//		}
//		santa.printState();
		System.out.println("...done");
		System.out.println();
		
		
	}
	
}
